package com.syl.practice.globalexception.common.restcontrolleradvice;

import com.syl.practice.globalexception.enums.ApiEnum;
import com.syl.practice.globalexception.exception.ApiException;
import com.syl.practice.globalexception.result.ApiResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *  全局处理方式2 - - 框架自带注解@RestControllerAdvice
 */
@RestControllerAdvice
public class MyRestController {

    @ExceptionHandler
    @ResponseBody
    public ApiResult excHandler(Exception ex) {
        if (ex instanceof ApiException) {
            return new ApiResult((ApiException) ex);
        }
        return new ApiResult(new ApiException(ApiEnum.ERROR));
    }
}
